
package com.gp.utils;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.util.Log;

/**
 * @author shijunxing
 * @className DatabaseManager
 * @date Aug 26, 2013 9:42:14 AM
 * @version 1.0
 * @Description 数据库操作类，对数据库各种操作的二次封装
 */
public class DatabaseManager {
    // 创建数据库的对象
    private DatabaseHelper mDatabaseHelper;
    // 操作数据库
    private SQLiteDatabase mSqLiteDatabase;

    private Context mContext = null;
    // 版本
    private int myVersion = 1;

    private String dbName = "notepad.db";

    public DatabaseManager( Context context ) {
        mContext = context;
    }

    // 打开数据库
    public void open() {
        try {
            mDatabaseHelper = new DatabaseHelper(mContext, dbName, null,
                    myVersion);
            if (mDatabaseHelper == null) {
                Log.v("mag", "it's null");
                return;
            }

            mSqLiteDatabase = mDatabaseHelper.getWritableDatabase();

        }
        catch (SQLiteException se) {
            se.printStackTrace();
        }
    }

    // 关闭数据库
    public void close() {
        mSqLiteDatabase.close();
        mDatabaseHelper.close();
    }

    // 获取列表cursor
    public Cursor selectNoteAll() {
        Cursor cursor = null; // cursor 一行数据的集合
        try {
            String sql = "select * from record";
            cursor = mSqLiteDatabase.rawQuery(sql, null);
        }
        catch (SQLiteException se) {
            se.printStackTrace();
            cursor = null;
        }
        return cursor;
    }

    
    public Cursor selectCityGuideAll() {
        Cursor cursor = null; // cursor 一行数据的集合
        try {
            String sql = "select * from cityguide";
            cursor = mSqLiteDatabase.rawQuery(sql, null);
        }
        catch (SQLiteException se) {
            se.printStackTrace();
            cursor = null;
        }
        return cursor;
    }
    //根据id查询
    public Cursor selectById(int id) {
        Cursor cursor = null;
        try {

            String sql = "select * from record where _id='" + id + "'";
            cursor = mSqLiteDatabase.rawQuery(sql, null);

        }
        catch (SQLiteException se) {
            se.printStackTrace();
            cursor = null;
        }
        return cursor;
    }

    // 插入数据
    public long insert(String title, String content) {
        long l = -1;
        long time = System.currentTimeMillis(); // 获取当前时间；
        try {

            ContentValues v = new ContentValues();
            v.put("title", title);
            v.put("content", content);
            v.put("time", time);
            l = mSqLiteDatabase.insert("record", null, v);

        }
        catch (Exception e) {
            e.printStackTrace();
            l = -1;
        }

        return l;
    }

    // 删除数据
    public int delete(long id) {
        int del = 0;
        try {

            del = mSqLiteDatabase.delete("record", "_id=?", new String[] { id
                    + "" });

        }
        catch (Exception e) {
            e.printStackTrace();
            del = -1;
        }
        return del;
    }

    // 修改
    public int update(int id, String title, String content) {
        int upDate = 0;
        try {

            ContentValues v = new ContentValues();
            v.put("title", title);
            v.put("content", content);
            // String[] w = {id+""};
            // String[] whereId = {Integer.toString(id)};
            upDate = mSqLiteDatabase.update("record", v, "_id=?",
                    new String[] { id + "" });
            // upDate = sqliteDatabase.update("record", v,"_id=?", whereId);
        }
        catch (Exception e) {
            e.printStackTrace();
            upDate = -1;
        }
        return upDate;

    }

}
